﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Sale
{
    public partial class 采购材料合同录入 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            int id = 0;
            int zid = 0;
            int bid = 0;
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }
                if (Request.QueryString["ZID"] != null)
                {
                    int.TryParse(Request.QueryString["ZID"], out zid);
                }
                if (Request.QueryString["BID"] != null)
                {
                    int.TryParse(Request.QueryString["BID"], out bid);
                }
                if (id > 0)
                {
                    this.HTID.Text = id.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = this.HTID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                if (zid > 0)
                {
                    this.Button_Cancel.Visible = true;
                    this.Button_Cancel.OnClientClick = "parent.swin2_load('/Sale/采购材料中标.aspx?id=" + bid + "',840,422);return false;";

                    this.Button_Close.Visible = true;

                    this.Date1.Text = DateTime.Today.ToLongDateString();
                    this.Date2.Text = this.Date1.Text;
                    this.ZBID.Text = zid.ToString();
                    this.SqlDataSource1.SelectParameters["中标ID"].DefaultValue = this.ZBID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (this.HTID.Text != "0")
            {
                if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
                {
                    this.BianHao.Text = (string)e.Command.Parameters[1].Value;
                    this.LBID.Text = ((int)e.Command.Parameters[3].Value).ToString();
                    this.GHID.Text = ((int)e.Command.Parameters[4].Value).ToString();

                    this.NianDu.Text = ((int)e.Command.Parameters[5].Value).ToString();
                    this.JiDu.Text = ((byte)e.Command.Parameters[6].Value).ToString();
                    this.NianJiDu.Text = this.NianDu.Text + "年" + this.JiDu.Text + "季度";
                    this.ShuLiang.Text = ((double)e.Command.Parameters[7].Value).ToString();
                    this.DanJia.Text = ((double)e.Command.Parameters[8].Value).ToString();
                    if (((double)e.Command.Parameters[9].Value).ToString() == "0")
                    {
                        this.ChangDu.Text = "——";
                    }
                    this.Date1.Text = ((DateTime)e.Command.Parameters[10].Value).ToLongDateString();
                    this.Date2.Text = ((DateTime)e.Command.Parameters[11].Value).ToLongDateString();
                    this.YunShu.Text = (string)e.Command.Parameters[12].Value;
                    this.BeiZhu.Text = (string)e.Command.Parameters[13].Value;
                    this.JiaoHuo.Text = ((double)e.Command.Parameters[14].Value).ToString();

                    //this.JiaoHuo1.Text = ((double)e.Command.Parameters[15].Value).ToString();
                    //this.JiaoHuo2.Text = ((double)e.Command.Parameters[16].Value).ToString();
                    //this.JiaoHuo3.Text = ((double)e.Command.Parameters[17].Value).ToString();
                    //this.JiaoHuo4.Text = ((double)e.Command.Parameters[18].Value).ToString();
                    //this.JiaoHuo5.Text = ((double)e.Command.Parameters[19].Value).ToString();
                    //this.JiaoHuo6.Text = ((double)e.Command.Parameters[20].Value).ToString();
                    //this.JiaoHuo7.Text = ((double)e.Command.Parameters[21].Value).ToString();
                    //this.JiaoHuo8.Text = ((double)e.Command.Parameters[22].Value).ToString();
                    //this.JiaoHuo9.Text = ((double)e.Command.Parameters[23].Value).ToString();

                    this.LeiBie.Text = (string)e.Command.Parameters[24].Value;
                    if (!Convert.IsDBNull(e.Command.Parameters[25].Value))
                    {
                        this.GuiGe.Text = (string)e.Command.Parameters[25].Value;
                    }
                    else
                    {
                        this.GuiGe.Text = "";
                    }
                    this.GongHuo.Text = (string)e.Command.Parameters[26].Value;
                    this.DanWei.Text = (string)e.Command.Parameters[27].Value;
                    
                }
            }
            else if (this.ZBID.Text != "0")
            {
                if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
                {
                    this.NianDu.Text = ((int)e.Command.Parameters[5].Value).ToString();
                    this.JiDu.Text = ((byte)e.Command.Parameters[6].Value).ToString();
                    this.NianJiDu.Text = this.NianDu.Text + "年" + this.JiDu.Text + "季度";
                    this.LBID.Text = ((int)e.Command.Parameters[3].Value).ToString();
                    this.LeiBie.Text = (string)e.Command.Parameters[24].Value;
                    if (!Convert.IsDBNull(e.Command.Parameters[25].Value))
                    {
                        this.GuiGe.Text = (string)e.Command.Parameters[25].Value;
                    }
                    else
                    {
                        this.GuiGe.Text = "";
                    }
                    this.GHID.Text = ((int)e.Command.Parameters[4].Value).ToString();
                    this.GongHuo.Text = (string)e.Command.Parameters[26].Value;
                    this.DanWei.Text = (string)e.Command.Parameters[27].Value;

                    this.ShuLiang.Text = ((double)e.Command.Parameters[7].Value).ToString();
                    this.JiaoHuo.Text = this.ShuLiang.Text;
                    this.DanJia.Text = ((double)e.Command.Parameters[8].Value).ToString();
                }
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            this.BianHao.Text = this.BianHao.Text.Trim();
            if (this.BianHao.Text == "")
            {
                this.Message.Text = "·请录入“合同编号”";
                return;;
            }
            DateTime d1, d2;
            if (!DateTime.TryParse(this.Date1.Text, out d1))
            {
                this.Message.Text = "·“签订日期”录入有误";
                return;;
            }
            if (!DateTime.TryParse(this.Date2.Text, out d2))
            {
                this.Message.Text = "·“交货时间”录入有误";
                return;;
            }
            decimal shuliang;
            if (!decimal.TryParse(this.ShuLiang.Text, out shuliang) || shuliang <= 0)
            {
                this.Message.Text = "·“数量”录入有误";
                return;;
            }
            decimal danjia;
            if (!decimal.TryParse(this.DanJia.Text, out danjia) || danjia <= 0)
            {
                this.Message.Text = "·“单价”录入有误";
                return;;
            }
            decimal changdu=0;
            //if (!decimal.TryParse(this.ChangDu.Text, out changdu) || changdu <= 0)
            //{
            //    this.Message.Text = "·“长度”录入有误";
            //    return; ;
            //}
            if (this.ChangDu.Text == "——")
            {
                changdu = 0;
            }
            int i;
            decimal[] jiaohuo = new decimal[10];
            decimal total = 0;
            if (!decimal.TryParse(this.JiaoHuo.Text, out jiaohuo[0]) || jiaohuo[0] <= 0)
            {
                this.Message.Text = "·“交货数量”录入有误";
                return;;
            }
            //for (i = 1; i < 10; i++)
            //{
            //    if (!decimal.TryParse(((TextBox)this.FindControl("JiaoHuo" + i)).Text, out jiaohuo[i]) || jiaohuo[i] < 0)
            //    {
            //        this.Message.Text = "·交货明细中存在无效数据或未填写";
            //        return;;
            //    }
            //    total += jiaohuo[i];
            //}
            //if (total != jiaohuo[0])
            //{
            //    this.Message.Text = "·交货明细总和(" + total + ")与交货数量不符(" + jiaohuo[0] + ")";
            //    return;;
            //}

            //开始保存数据
            if (this.HTID.Text != "0")
            {
                //编号,中标ID,材料ID,供货ID,年度,季度,数量,单价,长度,签订日期,交货时间,运输方式,备注,交货数量
                ParameterCollection pc = this.SqlDataSource1.UpdateParameters;
                pc["ID"].DefaultValue = this.HTID.Text;
                pc["编号"].DefaultValue = this.BianHao.Text;
                pc["数量"].DefaultValue = shuliang.ToString();
                pc["单价"].DefaultValue = danjia.ToString();
                pc["长度"].DefaultValue = changdu.ToString();
                pc["签订日期"].DefaultValue = d1.ToShortDateString();
                pc["交货时间"].DefaultValue = d2.ToShortDateString();
                pc["运输方式"].DefaultValue = this.YunShu.Text;
                pc["备注"].DefaultValue = this.BeiZhu.Text;
                pc["交货数量"].DefaultValue = jiaohuo[0].ToString();
                for (i = 1; i < 10; i++)
                {
                    pc["交货数量" + i].DefaultValue ="0";
                }
                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Message.Text = "·保存成功";
                }
                else
                {
                    this.Message.Text = "·保存失败";
                }
            }
            else if (this.ZBID.Text !="0")//新合同
            {
                //编号,中标ID,材料ID,供货ID,年度,季度,数量,单价,长度,签订日期,交货时间,运输方式,备注,交货数量
                ParameterCollection pc = this.SqlDataSource1.InsertParameters;
                pc["编号"].DefaultValue = this.BianHao.Text;
                pc["中标ID"].DefaultValue = this.ZBID.Text;
                pc["材料ID"].DefaultValue = this.LBID.Text;
                pc["供货ID"].DefaultValue = this.GHID.Text;
                pc["年度"].DefaultValue = this.NianDu.Text;
                pc["季度"].DefaultValue = this.JiDu.Text;
                pc["数量"].DefaultValue = shuliang.ToString();
                pc["单价"].DefaultValue = danjia.ToString();
                pc["长度"].DefaultValue = changdu.ToString();
                pc["签订日期"].DefaultValue = d1.ToShortDateString();
                pc["交货时间"].DefaultValue = d2.ToShortDateString();
                pc["运输方式"].DefaultValue = this.YunShu.Text;
                pc["备注"].DefaultValue = this.BeiZhu.Text;
                pc["交货数量"].DefaultValue = jiaohuo[0].ToString();
                for (i = 1; i < 10; i++)
                {
                    pc["交货数量"+i].DefaultValue = "0";
                }
                if (this.SqlDataSource1.Insert() > 0)
                {
                    this.Message.Text = "·保存成功";
                }
                else
                {
                    this.Message.Text = "·保存失败";
                }
            }
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[0].Value))
            {
                this.HTID.Text = ((int)e.Command.Parameters[0].Value).ToString();
            }
        }
    }
}
